// ++++++++++++++++++++++++++++++++++++++++
// 《零基础Go语言算法实战》源码
// ++++++++++++++++++++++++++++++++++++++++
// Author:廖显东（ShirDon）
// Blog:https://www.shirdon.com/
// Gitee:https://gitee.com/shirdonl/goAlgorithms.git
// Buy link :https://item.jd.com/14101229.html
// ++++++++++++++++++++++++++++++++++++++++

package main

import "fmt"

func searchClosestElements(array []int, k int, x int) []int {
	low, high := 0, len(array)-k
	for low < high {
		mid := low + (high-low)>>1
		if x-array[mid] > array[mid+k]-x {
			low = mid + 1
		} else {
			high = mid
		}
	}
	return array[low : low+k]
}

func main() {
	array := []int{1, 2, 4, 5, 6, 8, 99, 251, 320}
	res := searchClosestElements(array, 6, 1)
	fmt.Println(res)
}

//$ go run interview7-3.go
//[1 2 4 5 6 8]
